//
//  HLDoctorListTableViewController.m
//  NewKY
//
//  Created by 罗浩林 on 16/11/16.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "HLDoctorListTableViewController.h"
#import "NKFindTableViewCell.h"
#import "HLDoctorListModel.h"
#import "HLDoctorDetailsViewController.h"
#import <MJRefresh.h>
#import <SVProgressHUD.h>


static NSString * FindCellID = @"FindCell";

@interface HLDoctorListTableViewController ()

@property (nonatomic,strong) NSArray *dataArr;

/** 数据源数组 */
@property (nonatomic,strong) NSMutableArray *infoList;

@end

@implementation HLDoctorListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UINib *nib = [UINib nibWithNibName:@"NKFindTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"FindCell"];
    
    self.infoList = [NSMutableArray new];
    
    [self postMethod];
    
    //================== 上下拉刷新 =================//
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(netRequest)];

//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:nil];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(netRequest)];
    
    //设置弹框的属性
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
    
}


- (void)netRequest{
    
    //根据不同的类型选择不同的接口
    NSString *URLStr = @"";
    NSDictionary *parameters = [[NSDictionary alloc] init];
    
    URLStr = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
    parameters = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    //显示遮罩层
    [SVProgressHUD setInfoImage:[UIImage imageNamed:@"0"]];
    [SVProgressHUD showInfoWithStatus:@"正在加载医生列表..."];
    
    [NetWorkingManager sendPOSTDataWithPath:URLStr withParamters:parameters withProgress:^(float progress) {
        
    } success:^(BOOL isSuccess, id responseObject) {
        
        if ([responseObject isKindOfClass:[NSError class]]) {
            [SVProgressHUD showErrorWithStatus:@"您的网络有问题"];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            return ;
        }
        //字典转模型
        NSArray *dictArr = responseObject[@"data"];
        NSArray *arr = [NSArray modelArrayWithClass:[HLDoctorListModel class] json:dictArr];
        
        [self.infoList addObjectsFromArray:arr];
        
        //刷新数组
        [self.tableView reloadData];
        [SVProgressHUD showSuccessWithStatus:@"加载成功"];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        if (error != nil) {
            NSLog(@"%@",error);
        }
    }];
}



- (void)postMethod{
    
    //根据疾病得到医生数量接口
    NSString *url = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
    NSDictionary *parameters = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};

    
    [NetWorkingManager sendPOSTDataWithPath:url withParamters:parameters withProgress:^(float progress) {
        
    } success:^(BOOL isSuccess, id responseObject) {
        if(isSuccess){
            
            NSArray * dictArr = responseObject[@"data"];
            
            NSArray * ModelArr = [NSArray modelArrayWithClass:[HLDoctorListModel class] json:dictArr];
            
            _dataArr = ModelArr;
            
            [self.infoList addObjectsFromArray:ModelArr];
            //回到主线程刷新数据
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.tableView reloadData];
            }];
            
            [self.tableView reloadData];
            
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.infoList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NKFindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FindCellID forIndexPath:indexPath];
    
    HLDoctorListModel * model = self.infoList[indexPath.row];
    
    cell.doctorModel = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HLDoctorDetailsViewController * doctorDetail = [HLDoctorDetailsViewController new];
    
    [self.navigationController pushViewController:doctorDetail animated:YES];
    
}


@end
